<?php

require_once("Common/Model/CheckStrings.php");
require_once("Database/Model/WorkoutUpdateDAL.php");
require_once("login/src/Model/LoginValidation.php");
require_once("WorkoutUpdates/Model/WorkoutUpdate.php");
require_once("WorkoutUpdates/Model/WorkoutUpdateTitle.php");
require_once("WorkoutUpdates/Model/WorkoutUpdateComment.php");

class WorkoutUpdateModel
{
	/**
	*@var LoginValidation
	**/
	private $loginValidation;
	/**
	*@var WorkoutUpdateDAL
	**/
	private $workoutUpdateDAL;
	/**
	*@var string
	**/
	private static $idToEdit = "WorkoutUpdateModel::idToEdit";
	/**
	*@var string
	**/
	private static $errorMessage = "WorkoutUpdateModel::errorMessage";
	/**
	*@var string
	**/
	private static $succesMessage = "WorkoutUpdateModele::succesMessage";

	public function __construct()
	{
		$this->loginValidation = new LoginValidation();
		$this->workoutUpdateDAL = new WorkoutUpdateDAL();
	}

	/**
    *@return int
    **/	
	public function getIdToEdit()
	{
		if(isset($_SESSION[self::$idToEdit]))
		{
			return $_SESSION[self::$idToEdit];
		}
		return 0;
	} 

	/**
	*@param int
	**/
	public function setIdToEdit($id)
	{
		$_SESSION[self::$idToEdit] = $id;
	}

	/**
	*@return int
	**/
	public function getErrorMessage()
	{
		return $_SESSION[self::$errorMessage];
	}

	/**
	*@return string
	**/
	public function getSuccessMessage()
	{
		return $_SESSION[self::$succesMessage];
	}

	public function resetMessages()
	{
		$_SESSION[self::$succesMessage] = "";
		$_SESSION[self::$errorMessage] ="";
	}

	/**
	*@param string
	*@param string
	*@param int
	*@param int
	*@return bool
	**/
	public function ValidateWorkoutUpdate($title, $comment, $workoutID, $updateID = null)
	{
		$validTitle = $this->ValidateTitle($title);
		$noTags = $this->areThereTags($title, $comment);
		$validComment = $this->ValidateComment($comment);
		
		if($validTitle && $validComment && !$noTags)
		{
			return true;
		}
		return false;
	}

	/**
	*@param string
	*@param string
	*@return bool
	**/
	private function areThereTags($title, $comment)
	{	
		$bool= false;
		if(CheckStrings::hasTags($title))
		{
			$_SESSION[self::$errorMessage] .= "Titeln innehåller ogiltiga tecken, tex < > ...";
			$bool = true;
		}
		elseif(CheckStrings::hasTags($comment))
		{
			$_SESSION[self::$errorMessage] .= "Kommentaren innehåller ogiltiga tecken, tex < >...";
			$bool = true;
		}
		return $bool;
	}

	/**
	*@param string
	*@return bool
	**/
	private function ValidateTitle($title)
	{
		$bool = true;
		if(strlen($title) < WorkoutUpdateTitle::Min_Title_Length)
		{
			$_SESSION[self::$errorMessage] .= "Du har glömt att fylla i en titel...";
			$bool = false;
		}
		elseif(strlen($title) > WorkoutUpdateTitle::Max_Title_Length)
		{
			$_SESSION[self::$errorMessage] .= "titeln får max vara 40 tecken...";
			$bool = false;
		}
		return $bool;
	}

	/**
	*@param string
	*@return bool
	**/
	private function ValidateComment($comment)
	{
		$bool = true;
		if(strlen($comment) < WorkoutUpdateComment::Min_Comment_Length)
		{
			$_SESSION[self::$errorMessage] .= "Du har glömt att fylla i en kommentar om ditt träningspass...";
			$bool = false; 
		}
		elseif(strlen($comment) > WorkoutUpdateComment::Max_Comment_Length)
		{
			$_SESSION[self::$errorMessage] .= "Kommentaren får max vara 1000tecken...";
			$bool = false;  
		}
		return $bool;
	}

	/**
	*@param int
	**/
	public function ValidateAndDeleteUpdate($workoutID)
	{ 
		$update = $this->workoutUpdateDAL->getUpdates($workoutID);
		if(isset($update))
		{  
			$userID = $this->loginValidation->getLoggedInUserID();
			if($update->CheckIfSameUserID($userID))
			{  
				$this->workoutUpdateDAL->DeleteUpdate($workoutID);
				$_SESSION[self::$succesMessage] = "Uppdateringen har tagits bort";
			}
			else
			{ 
				$_SESSION[self::$errorMessage] = "   Du har ej rättigheter att ta bort den uppdateringen   .";
			}
		}
		else
		{	
			$_SESSION[self::$errorMessage] = "   Ett fel uppstod vid borttagning av uppdateringen   .";
		}
	}


	/**
	*@param string
	*@param string
	*@param int
	*@param int
	**/
	public function createAndSaveWorkoutUpdate($title, $comment, $workoutID, $workoutUpdateID = null)
	{
		if($workoutUpdateID == null)
		{
			$workoutUpdateToSave = WorkoutUpdate::createNewWorkoutUpdate($title, $comment,  $workoutID);
			$this->workoutUpdateDAL->saveNewWorkoutUpdate($workoutUpdateToSave);
			$_SESSION[self::$succesMessage] = "Uppdateringen har lagts till";
		}
		else
		{	 
			$workoutUpdateToSave = WorkoutUpdate::createNewWorkoutUpdate($title, $comment, $workoutID, $workoutUpdateID);
			$this->workoutUpdateDAL->EditWorkoutUpdate($workoutUpdateToSave, $workoutUpdateID);
			$_SESSION[self::$succesMessage] = "Uppdateringen har redigerats";
		}
	}


}